﻿using GBlog.Core.Service;

namespace System.Web.Mvc
{
    public static class UrlHelperExtensions
    {
        public static string ThemeViewBasePath(this UrlHelper url)
        {
            string area = string.Empty;
            if (url.RequestContext.RouteData.Values.ContainsKey("area"))
            {
                area = url.RequestContext.RouteData.Values["area"] as string;
            }
            string rootPath = url.ThemeRootPath();
            return string.Format("{0}{1}Views/", rootPath, string.IsNullOrWhiteSpace(area) ? "" : string.Format("areas/{0}/", area));
        }

        private static string GetTheme()
        {
            IThemeService service = DependencyResolver.Current.GetService<IThemeService>();
            if (service != null)
            {
                return service.GetCurrentTheme();
            }
            return string.Empty;
        }

        public static string ContentPath(this UrlHelper url)
        {
            return string.Format("{0}{1}", url.ThemeRootPath(), "content/");
        }

        public static string ThemeRootPath(this UrlHelper url)
        {
            string theme = GetTheme();
            if (string.IsNullOrEmpty(theme))
            {
                return string.Format("~/");
            }
            else
            {
                return string.Format("~/themes/{0}/", theme);
            }
        }
    }
}